/*================================================================
*   Copyright (C) 2018  All rights reserved.
*   
*   Filename：tcp_client.c
*   Creator ：pony
*   Create  date：2018-01-28
*   Description：
*
================================================================*/

#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>

#define SERVER_PORT 6666

int main(int argc, char** argv)
{
	struct sockaddr_in serveraddr;
	int sockfd;
	int n;
	int ret;
	char buff[] = "helloworld";

	//create sock
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	bzero(&serveraddr,sizeof(serveraddr));

	serveraddr.sin_family = AF_INET;
	inet_pton(AF_INET,"127.0.0.1",&serveraddr.sin_addr);
	serveraddr.sin_port = htons(SERVER_PORT);
	
	int conntimes = 0;
	//connect
	while(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) == -1)
	{
		if(conntimes++ > 10)
		{
			return -1;
		}
		sleep(1);
		perror("connect");
	}
	
	int num = 0;
	while(1)
	{
		//scanf("%s",buff);
		ret = write(sockfd,buff,strlen(buff));
		printf("ret = %d\n",ret);
		if(-1 == ret)
		{
			perror("write");
		}
		else
		{
			num += ret;
		}
		printf("send bypes %d\n",num);
		sleep(1);
	}
	close(sockfd);


	return 0;
}
